﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMSSystem.DYB;
using WMSSystem.YJ.DTO.Input;
using WMSSystem.YJ.DTO.Out;
using WMSSystem.YJ.Interface;
using static Microsoft.FSharp.Core.ByRefKinds;

namespace WMSSystem.YJ
{
    public class InventorytableAppService : ApplicationService, IInventorytableAppService
    {
        private readonly IRepository<Inventorytable, Guid> _inventiorytableRepository;
        private readonly IRepository<InventoryGoods, Guid> _inventioryGoodsRepository;
        private readonly IRepository<Commoditiestable, Guid> _commoditiestableRepository;

        public InventorytableAppService(IRepository<Inventorytable, Guid> inventiorytableRepository, IRepository<InventoryGoods, Guid> inventioryGoodsRepository, IRepository<Commoditiestable, Guid> commoditiestableRepository)
        {
            _inventiorytableRepository = inventiorytableRepository;
            _inventioryGoodsRepository = inventioryGoodsRepository;
            _commoditiestableRepository = commoditiestableRepository;
        }
        /// <summary>
        /// 1.创建库存表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task<int> CreateInventorytable(CreateInventorytableDTO input)
        {
            var i = 0;
            foreach (var item in input.Data)
            {
                var data = ObjectMapper.Map<AddData, Inventorytable>(item);
                var res= _inventiorytableRepository.InsertAsync(data);
                if (i != null)
                {
                    i = i + 1;
                }
                else
                {
                    i = 0;
                }
               
            }
            if (i == input.Data.Count())
            {
                return Task.FromResult(200);
            }
            else
            {
                return Task.FromResult(500);
            }
        }
        /// <summary>
        /// 库存查询
        /// </summary>
        /// <param name="PositionManagementId"></param>
        /// <returns></returns>
        public async Task<List<PagingDataOut>> PagingInventory(Guid PositionManagementId)
        {
            IQueryable<InventoryGoods> ss = await _inventioryGoodsRepository.GetQueryableAsync();
          var data=ObjectMapper.Map<List<InventoryGoods>, List<PagingDataOut>>(ss.ToList());
            List<PagingDataOut> data2 = new List<PagingDataOut>();
            if (PositionManagementId.ToString()!=null)
            {
                data2 = data.Where(x => x.PositionManagementId.ToString()==(PositionManagementId.ToString())).ToList();
             }

            foreach (var item in data2)
            {
                var list = await _commoditiestableRepository.GetQueryableAsync();
                var com = list.Where(x => x.Id == item.CommoditiesId).FirstOrDefault();
                if (com != null)
                {
                    item.CommoditiesName = com.CommoditiesName;
                }
               
            }
            return data2;
        }
             /// <summary>
             /// 获取库存
             /// </summary>
             /// <returns></returns>
        public async Task<List<PagingDataOut>> PagingInventory222()
        {
            IQueryable<InventoryGoods> ss = await _inventioryGoodsRepository.GetQueryableAsync();
            var data = ObjectMapper.Map<List<InventoryGoods>, List<PagingDataOut>>(ss.ToList());
            
            foreach (var item in data)
            {
                var list = await _commoditiestableRepository.GetQueryableAsync();
                var com = list.Where(x => x.Id == item.CommoditiesId).FirstOrDefault();
                if (com != null)
                {
                    item.CommoditiesName = com.CommoditiesName;
                }
            }
            return data;
        }
        /// <summary>
        /// 修改库存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> UpdateInventorytable(CreateInventorytableDTO input)
        {
            throw new NotImplementedException();
        }
    }
}
